package com.vtuzx.kingdee.xqc_core.dao;

import com.mongodb.client.result.InsertManyResult;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 即时库存分录Dao
 */
@Component
public class InventoryDetailDao extends VtuzxDaoImpl {
    /** 集合名 */
    public static final String NAME = "t_inventory_detail";
    /**
     * 构造
     */
    public InventoryDetailDao() {
        super(NAME);
    }

    /**
     * 插入履历
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param documents 对象列表
     * @return insertId列表
     */
    public List<ObjectId> insertDetail(ObjectId _userId, String userName, List<Document> documents) {
        Date now = new Date();
        for(Document doc: documents) {
            doc.put("_createUserId", _userId);
            doc.put("createUserName", userName);
            doc.put("createTime", now);
            doc.put("_updateUserId", _userId);
            doc.put("updateUserName", userName);
            doc.put("updateTime", now);
        }
        InsertManyResult res = this.getCollection(null).insertMany(documents);
        List<ObjectId> _insertIds = new ArrayList<>();
        Map<Integer, BsonValue> insertIdMap = res.getInsertedIds();
        for(int i = 0; i < documents.size(); i++) {
            _insertIds.add(insertIdMap.get(0).asObjectId().getValue());
        }
        return _insertIds;
    }

    public Iterator<Document> stockCount(Criteria cond) {
        List<Document> docList = new ArrayList<>();
//        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$lookup", new Document("from", InventoryDao.NAME).append("foreignField", "_id").append("localField", "_inventoryId").append("as", "inventory")));
        docList.add(new Document("$unwind", new Document("path", "$inventory")));
        docList.add(new Document("$match", cond.getCriteriaObject()));
        docList.add(new Document("$group",
                new Document("_id", new Document("stockNo", "$stockNo").append("materialNo", "$inventory.materialNo"))
                        .append("totalBoxCount", new Document("$sum", 1)).append("totalCount", new Document("$sum", "$count"))));
        return this.aggregate(docList);
    }

    public Iterator<Document> trayGroup(Criteria cond) {
        Document groupId = new Document("stockNo", "$stockNo").append("trayNumber", "$trayNumber").append("locationNo","$locationNo");
        List<Document> pipes = new ArrayList<>();
        pipes.add(new Document("$match", cond.getCriteriaObject()));
        pipes.add(new Document("$group", new Document(IVtuzxConst.Key__id, groupId).append("count", new Document("$sum", 1))
                .append("sumCount", new Document("$sum", "$count"))
                .append("locationNo", new Document("$max", "$locationNo"))));
        return aggregate(pipes);
    }

}